package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.rfid.BindDmcRfidReaderDto;
import com.csun.cmny.provider.model.dto.rfid.RfidReaderPageQuery;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidReaderVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcControllerCardBindService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcRfidReaderGroupBindService;
import com.csun.cmny.provider.service.DmcRfidReaderLocationService;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 13:24
 * @Description: the class dmc device rfid reader controller.
 */
@RestController
@RequestMapping(value = "/rfidReader", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcRfidReaderController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcRfidReaderLocationService dmcRfidReaderLocationService;

    @Resource
    private DmcRfidReaderGroupBindService dmcRfidReaderGroupBindService;
    @Resource
    private DmcControllerCardBindService dmcControllerCardBindService;

    /**
     * 查询组织门禁读卡器设备列表
     *
     * @param rfidReaderPageQuery   the rfid reader page query
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询组织门禁读卡器设备列表")
    @RequestMapping(value = "/queryGroupRfidReaderListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupRfidReaderListWithPage(@RequestBody RfidReaderPageQuery rfidReaderPageQuery) {

        rfidReaderPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        rfidReaderPageQuery.setDeviceType(DmcDeviceTypeEnum.RFID_READER.getIndex());

        PageInfo pageInfo = dmcRfidReaderLocationService.queryRfidReaderListWithPage(rfidReaderPageQuery);
        for (Object o : pageInfo.getList()) {
            DmcRfidReaderVo dmcRfidReaderVo = (DmcRfidReaderVo) o;
            if (!PubUtils.isNull(dmcRfidReaderVo.getLocation())) {
                dmcRfidReaderVo.setBind(true);
            }
        }

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加门禁读卡器设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加门禁读卡器设备")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addRfidReader(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.RFID_READER.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改门禁读卡器设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改门禁读卡器设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editRfidReader(@Validated(value = {DmcDeviceEditValidator.class})
                                 @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.RFID_READER.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除门禁读卡器
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除门禁读卡器")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcRfidReaderById(@PathVariable Long id) {

        // 校验是否绑定门禁组
        if (!dmcRfidReaderGroupBindService.checkRfidReaderBindGroupById(id)) {
            // 已经绑定门禁组 无法删除
            throw new DmcBizException(ErrorCodeEnum.DMC10064003);
        }

        Integer result = dmcDeviceService.deleteDmcDeviceById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("operation.failed",null));
        }

      //  return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 门禁读卡器绑定
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/bindDmcRfidReader", method = RequestMethod.POST)
    public Wrapper bindDmcRfidReader(@Validated @RequestBody BindDmcRfidReaderDto bindDmcRfidReaderDto,
                                     BindingResult br) {

        Integer result = dmcRfidReaderLocationService.bindDmcRfidReader(bindDmcRfidReaderDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);

    }

    /**
     * 门禁读卡器解绑
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/breakDmcRfidReader/{deviceId}", method = RequestMethod.POST)
    public Wrapper breakDmcRfidReader(@PathVariable Long deviceId) {

        // 校验是否绑定门禁组
        if (!dmcRfidReaderGroupBindService.checkRfidReaderBindGroupById(deviceId)) {
            // 已经绑定门禁组 无法解绑
            throw new DmcBizException(ErrorCodeEnum.DMC10064003);
        }

        // 校验是否绑定门禁读卡器
        if (!dmcControllerCardBindService.checkRfidReaderBindControllerCardById(deviceId)) {
            // 已经绑定门禁组 无法解绑
            throw new DmcBizException(ErrorCodeEnum.DMC10064007);
        }

        Integer result = dmcRfidReaderLocationService.breakDmcRfidReader(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

}
